package com.systemframework.sys.service.impl;

import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.Contain;
import cn.zhxu.bs.util.MapBuilder;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systemframework.sys.dto.request.EmailTemplateRequestDTO;
import com.systemframework.sys.dto.response.EmailTemplateResponseDTO;
import com.systemframework.sys.entity.EmailTemplate;
import com.systemframework.sys.mapper.EmailTemplateMapper;
import com.systemframework.sys.service.EmailTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author summer
 * @date 2023-04-06 10:02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EmailTemplateServiceImpl extends ServiceImpl<EmailTemplateMapper, EmailTemplate> implements EmailTemplateService {

    private BeanSearcher beanSearcher;

    @Autowired
    public void setBeanSearcher(BeanSearcher beanSearcher) {
        this.beanSearcher = beanSearcher;
    }

    @Override
    public Page<EmailTemplateResponseDTO> findLists(EmailTemplateRequestDTO emailTemplateRequestDTO) {
        MapBuilder page = MapUtils.builder().page(emailTemplateRequestDTO.getPageNum()-1, emailTemplateRequestDTO.getPageSize())
                .field(EmailTemplateResponseDTO::getName,emailTemplateRequestDTO.getName()).op(Contain.class)
                .field(EmailTemplateResponseDTO::getCode,emailTemplateRequestDTO.getCode()).op(Contain.class) ;
        SearchResult<EmailTemplateResponseDTO> search = beanSearcher.search(EmailTemplateResponseDTO.class, page.build());
        Page<EmailTemplateResponseDTO> page1=new Page<>(emailTemplateRequestDTO.getPageNum(), emailTemplateRequestDTO.getPageSize());
        page1.setRecords(search.getDataList());
        page1.setTotal(search.getTotalCount().longValue());
        return page1;
    }
}
